package cn.dimao.dhd.service.impl;

import cn.dimao.dhd.mapper.UserMapper;
import cn.dimao.dhd.pojo.UserTb;
import cn.dimao.dhd.pojo.vo.CommonResult;
import cn.dimao.dhd.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

/**
 * @author 15543
 * @version 1.0
 * @project text
 * @description
 * @date 2024/6/1 17:07:51
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper mapper;

    @Override
    public CommonResult selectUserTbById(Integer id) {
        UserTb userTb = mapper.selectById(id);
        if (userTb == null){
            return CommonResult.error("请求失败").putDate("无id为"+id+"的记录");
        }else {
            return CommonResult.ok("suc").putDate(userTb);
        }
    }

    @Override
    public CommonResult loginByPhone(String phone, String password) {
        QueryWrapper<UserTb> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserTb::getPhone,phone);
        UserTb userTb = mapper.selectOne(queryWrapper);
        //判断根据手机号查询用户记录是否为空
        if (userTb == null){
            return CommonResult.error("登陆失败").putDate("当前用户不存在");
        }else {
            //如果密码相同
            if (userTb.getPassword().equals(password)){
                return CommonResult.ok("suc").putDate("登陆成功");
            }else {
                return CommonResult.error("登陆失败").putDate("密码错误");
            }
        }
    }


    @Override
    public CommonResult deleteUserTbById(Integer id) {
        int userTb = mapper.deleteById(id);
        if (userTb == 1){
            return CommonResult.ok("suc").putDate("删除成功");
        }else {
            return CommonResult.error("请求失败").putDate("无法删除记录");
        }
    }


    @Override
    public CommonResult saveUserTb(UserTb userTb) {
        int i = mapper.insert(userTb);
        if (i == 1) {
            return CommonResult.ok("添加成功").putDate(userTb);
        } else {
            return CommonResult.error("请求失败").putDate("无法添加记录");
        }
    }


    @Override
    public CommonResult updateUserTb(UserTb userTb) {
        int i = mapper.updateById(userTb);
        if (i == 1) {
            return CommonResult.ok("修改成功").putDate(userTb);
        } else {
            return CommonResult.error("请求失败").putDate("无法修改记录");
        }
    }
}